<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 15;
	}
	$total='';
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'limit'=>array($pindex,$psize),
	);
	$list=pdo_getslice('jy_saleapp_list',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url2']='jy_saleapp/getapp//id='.$item['id'];
			$item['url']=custom_url('jy_saleapp/getapp//id='.$item['id'],true);
			$item['url3']='jy_saleapp/appregister//id='.$item['id'];
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'APP列表'),
			'1'=>array('op'=>'edit','title'=>'添加APP'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('type'=>'string','title'=>'排序','name'=>'paixu'),
			array('name'=>'logo','title'=>'APP图标','type'=>'image'),
			array('name'=>'title','title'=>'APP标题','type'=>'string'),
			array('type'=>'unitstring','unit'=>'次','title'=>'浏览量','name'=>'readnum'),
			array('type'=>'unitstring','unit'=>'次','title'=>'分享量','name'=>'salenum'),
			array('type'=>'unitstring','unit'=>'次','title'=>'下载量','name'=>'downnum'),
			array('name'=>'url','title'=>'APP分享地址','type'=>'copy'),
			array('name'=>'url','title'=>'APP分享二维码','type'=>'ewm'),
			array('name'=>'url2','title'=>'APP分享短地址','type'=>'copy'),
			array('name'=>'url3','title'=>'APP下载短地址','type'=>'copy'),
			array('type'=>'switch','title'=>'状态','name'=>'status', 'set'=>'set'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_saleapp_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['saleset']=json_decode($item['saleset'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'APP',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'APP列表'),
			'1'=>array('op'=>'edit','title'=>'添加APP'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'标题','name'=>'title', 'help'=>'' ),
			array('type'=>'image','title'=>'图标','name'=>'logo', 'help'=>'' ),
			array('type'=>'text','title'=>'描述','name'=>'desc', 'help'=>'' ),
			array('type'=>'switch','title'=>'支持多注册','name'=>'is_morezc', 'help'=>'开启后，每次不是注册下载，而是先注册多个账户，注册好后，再点下载APP' ),
			array('type'=>'upfile','title'=>'上传安卓APP安装包','name'=>'installbak'),
			array('type'=>'upfile','title'=>'上传苹果APP安装包','name'=>'installbak2'),
			array('type'=>'array','name'=>'saleset','title'=>'邀请奖','add_tip'=>'添加一个邀请奖',
				'list'=>array(
						array('type'=>'radio','title'=>'计算规则','name'=>'yao_jitype','options'=>'yao_jitype'),
						array('type'=>'string','name'=>'num','title'=>'邀请人数','show'=>array('yao_jitype'=>0)),
						array('type'=>'radio','title'=>'奖励类型','name'=>'type','options'=>'credit_types'),
						array('type'=>'string','name'=>'fee','title'=>'奖励额度'),							
				),
				'details'=>array(
						array('type'=>'radio','title'=>'计算规则','name'=>'yao_jitype','options'=>'yao_jitype'),
						array('type'=>'string','name'=>'num','title'=>'邀请人数','show'=>array('yao_jitype'=>0)),
						array('type'=>'radio','title'=>'奖励类型','name'=>'type','options'=>'credit_types'),
						array('type'=>'string','name'=>'fee','title'=>'奖励额度'),	
				)
			),
			array('type'=>'string','title'=>'排序','name'=>'paixu', 'help'=>'' ),
			array('type'=>'switch','title'=>'状态','name'=>'status', 'help'=>'' ),
		)
	);
	$return['credit_types']=credit_types();
	$return['yao_jitype']=array('0'=>'阶梯式','1'=>'每人式');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'logo'=>trim($postdata['logo']),
		'installbak'=>trim($postdata['installbak']),
		'installbak2'=>trim($postdata['installbak2']),
		'paixu'=>trim($postdata['paixu']),
		'saleset'=>ijson_encode($postdata['saleset']),
		'is_morezc'=>trim($postdata['is_morezc']),
		'status'=>trim($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_saleapp_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('APP修改成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_saleapp_list',$data);
		exi('APP增加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['staus'])){
		$update['staus']=intval($post['staus']);
	}
	if($update){
		pdo_update('jy_saleapp_list',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_saleapp_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_saleapp_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}

?>